/**
 * 属性计算工具类
 * 提供前端实时属性计算功能
 */

// 属性常量配置
const ATTRIBUTE_CONSTANTS = {
  // 基础属性范围
  HEALTH_MIN: 100,
  HEALTH_MAX: 10000,
  DEFENSE_MIN: 10,
  DEFENSE_MAX: 1000,
  SPEED_MIN: 50,
  SPEED_MAX: 500,
  BASE_DAMAGE_MIN: 20,
  BASE_DAMAGE_MAX: 2000,
  
  // 进阶属性范围
  DAMAGE_INCREASE_MIN: 0,
  DAMAGE_INCREASE_MAX: 50,
  DAMAGE_REDUCTION_MIN: 0,
  DAMAGE_REDUCTION_MAX: 40,
  CRIT_RATE_MIN: 5,
  CRIT_RATE_MAX: 35,
  ABNORMAL_RESIST_MIN: 10,
  ABNORMAL_RESIST_MAX: 60,
  CONTROL_SUCCESS_MIN: 5,
  CONTROL_SUCCESS_MAX: 40,
  DODGE_RATE_MIN: 5,
  DODGE_RATE_MAX: 30,
  HIT_RATE_MIN: 80,
  HIT_RATE_MAX: 120,
  
  // 基础属性增量
  HEALTH_PER_CONSTITUTION: 50,
  DEFENSE_PER_CONSTITUTION: 5,
  DAMAGE_PER_STRENGTH: 10,
  SPEED_PER_AGILITY: 3,
  
  // 进阶属性影响系数
  CRIT_RATE_PER_AGILITY: 0.1,
  ABNORMAL_RESIST_PER_CONSTITUTION: 0.2,
  CONTROL_SUCCESS_PER_STRENGTH: 0.1
}

/**
 * 属性计算器类
 */
class AttributeCalculator {
  constructor() {
    this.constants = ATTRIBUTE_CONSTANTS
  }
  
  /**
   * 计算基础属性
   * @param {Object} baseAttrs - 基础加点属性 {constitution, strength, agility}
   * @param {Object} bonus - 装备和技能加成 {equipment: {}, skill: {}}
   * @returns {Object} 计算后的基础属性
   */
  calculateBaseAttributes(baseAttrs, bonus = {}) {
    const { constitution = 0, strength = 0, agility = 0 } = baseAttrs
    const equipmentBonus = bonus.equipment || {}
    const skillBonus = bonus.skill || {}
    
    // 计算生命值
    const baseHealth = this.constants.HEALTH_MIN + 
      constitution * this.constants.HEALTH_PER_CONSTITUTION
    const health = this.clamp(
      baseHealth + (equipmentBonus.health || 0) + (skillBonus.health || 0),
      this.constants.HEALTH_MIN,
      this.constants.HEALTH_MAX
    )
    
    // 计算防御
    const baseDefense = this.constants.DEFENSE_MIN + 
      constitution * this.constants.DEFENSE_PER_CONSTITUTION
    const defense = this.clamp(
      baseDefense + (equipmentBonus.defense || 0) + (skillBonus.defense || 0),
      this.constants.DEFENSE_MIN,
      this.constants.DEFENSE_MAX
    )
    
    // 计算基础伤害
    const baseDamage = this.constants.BASE_DAMAGE_MIN + 
      strength * this.constants.DAMAGE_PER_STRENGTH
    const damage = this.clamp(
      baseDamage + (equipmentBonus.baseDamage || 0) + (skillBonus.baseDamage || 0),
      this.constants.BASE_DAMAGE_MIN,
      this.constants.BASE_DAMAGE_MAX
    )
    
    // 计算速度
    const baseSpeed = this.constants.SPEED_MIN + 
      agility * this.constants.SPEED_PER_AGILITY
    const speed = this.clamp(
      baseSpeed + (equipmentBonus.speed || 0) + (skillBonus.speed || 0),
      this.constants.SPEED_MIN,
      this.constants.SPEED_MAX
    )
    
    return {
      health,
      defense,
      baseDamage: damage,
      speed
    }
  }
  
  /**
   * 计算进阶属性
   * @param {Object} baseAttrs - 基础加点属性
   * @param {Object} bonus - 装备和技能加成
   * @returns {Object} 计算后的进阶属性
   */
  calculateAdvancedAttributes(baseAttrs, bonus = {}) {
    const { constitution = 0, strength = 0, agility = 0 } = baseAttrs
    const equipmentBonus = bonus.equipment || {}
    const skillBonus = bonus.skill || {}
    
    // 增伤 = 装备加成 + 技能加成
    const damageIncrease = this.clamp(
      (equipmentBonus.damageIncrease || 0) + (skillBonus.damageIncrease || 0),
      this.constants.DAMAGE_INCREASE_MIN,
      this.constants.DAMAGE_INCREASE_MAX
    )
    
    // 减伤 = 装备加成 + 技能加成
    const damageReduction = this.clamp(
      (equipmentBonus.damageReduction || 0) + (skillBonus.damageReduction || 0),
      this.constants.DAMAGE_REDUCTION_MIN,
      this.constants.DAMAGE_REDUCTION_MAX
    )
    
    // 暴击率 = 基础值 + 装备加成 + 技能加成 + 敏捷影响
    const agilityBonus = agility * this.constants.CRIT_RATE_PER_AGILITY
    const critRate = this.clamp(
      this.constants.CRIT_RATE_MIN + 
      (equipmentBonus.critRate || 0) + 
      (skillBonus.critRate || 0) + 
      agilityBonus,
      this.constants.CRIT_RATE_MIN,
      this.constants.CRIT_RATE_MAX
    )
    
    // 抗异常 = 基础值 + 装备加成 + 技能加成 + 体质影响
    const constitutionBonus = constitution * this.constants.ABNORMAL_RESIST_PER_CONSTITUTION
    const abnormalResist = this.clamp(
      this.constants.ABNORMAL_RESIST_MIN + 
      (equipmentBonus.abnormalResist || 0) + 
      (skillBonus.abnormalResist || 0) + 
      constitutionBonus,
      this.constants.ABNORMAL_RESIST_MIN,
      this.constants.ABNORMAL_RESIST_MAX
    )
    
    // 控制成功率 = 基础值 + 装备加成 + 技能加成 + 力量影响
    const strengthBonus = strength * this.constants.CONTROL_SUCCESS_PER_STRENGTH
    const controlSuccess = this.clamp(
      this.constants.CONTROL_SUCCESS_MIN + 
      (equipmentBonus.controlSuccess || 0) + 
      (skillBonus.controlSuccess || 0) + 
      strengthBonus,
      this.constants.CONTROL_SUCCESS_MIN,
      this.constants.CONTROL_SUCCESS_MAX
    )
    
    // 闪避率 = 基础值 + 装备加成 + 技能加成
    const dodgeRate = this.clamp(
      this.constants.DODGE_RATE_MIN + 
      (equipmentBonus.dodgeRate || 0) + 
      (skillBonus.dodgeRate || 0),
      this.constants.DODGE_RATE_MIN,
      this.constants.DODGE_RATE_MAX
    )
    
    // 命中率 = 基础值 + 装备加成 + 技能加成
    const hitRate = this.clamp(
      this.constants.HIT_RATE_MIN + 
      (equipmentBonus.hitRate || 0) + 
      (skillBonus.hitRate || 0),
      this.constants.HIT_RATE_MIN,
      this.constants.HIT_RATE_MAX
    )
    
    return {
      damageIncrease,
      damageReduction,
      critRate,
      abnormalResist,
      controlSuccess,
      dodgeRate,
      hitRate
    }
  }
  
  /**
   * 计算战斗力
   * @param {Object} baseAttrs - 基础属性
   * @param {Object} advancedAttrs - 进阶属性
   * @returns {number} 战斗力数值
   */
  calculateCombatPower(baseAttrs, advancedAttrs) {
    const {
      health = 0,
      defense = 0,
      baseDamage = 0,
      speed = 0
    } = baseAttrs
    
    const {
      damageIncrease = 0,
      damageReduction = 0,
      critRate = 0,
      abnormalResist = 0,
      controlSuccess = 0,
      dodgeRate = 0,
      hitRate = 0
    } = advancedAttrs
    
    // 战斗力计算公式（可根据游戏平衡性调整权重）
    const combatPower = Math.floor(
      health * 0.5 +
      defense * 2 +
      baseDamage * 3 +
      speed * 1 +
      damageIncrease * 10 +
      damageReduction * 15 +
      critRate * 8 +
      abnormalResist * 5 +
      controlSuccess * 6 +
      dodgeRate * 7 +
      hitRate * 2
    )
    
    return Math.max(combatPower, 0)
  }
  
  /**
   * 计算所有属性
   * @param {Object} baseAttrs - 基础加点属性
   * @param {Object} bonus - 装备和技能加成
   * @returns {Object} 完整的属性数据
   */
  calculateAllAttributes(baseAttrs, bonus = {}) {
    const basicAttrs = this.calculateBaseAttributes(baseAttrs, bonus)
    const advancedAttrs = this.calculateAdvancedAttributes(baseAttrs, bonus)
    const combatPower = this.calculateCombatPower(basicAttrs, advancedAttrs)
    
    return {
      // 基础加点属性
      constitution: baseAttrs.constitution || 0,
      strength: baseAttrs.strength || 0,
      agility: baseAttrs.agility || 0,
      
      // 基础属性
      ...basicAttrs,
      
      // 进阶属性
      ...advancedAttrs,
      
      // 战斗力
      combatPower
    }
  }
  
  /**
   * 预览属性变化
   * @param {Object} currentAttrs - 当前属性
   * @param {Object} changes - 属性变化 {constitution, strength, agility}
   * @param {Object} bonus - 装备和技能加成
   * @returns {Object} 预览后的属性数据
   */
  previewAttributeChanges(currentAttrs, changes, bonus = {}) {
    const newBaseAttrs = {
      constitution: (currentAttrs.constitution || 0) + (changes.constitution || 0),
      strength: (currentAttrs.strength || 0) + (changes.strength || 0),
      agility: (currentAttrs.agility || 0) + (changes.agility || 0)
    }
    
    return this.calculateAllAttributes(newBaseAttrs, bonus)
  }
  
  /**
   * 获取属性增量信息
   * @param {string} attrType - 属性类型
   * @returns {Object} 属性增量信息
   */
  getAttributeIncrement(attrType) {
    const increments = {
      constitution: {
        health: this.constants.HEALTH_PER_CONSTITUTION,
        defense: this.constants.DEFENSE_PER_CONSTITUTION,
        abnormalResist: this.constants.ABNORMAL_RESIST_PER_CONSTITUTION
      },
      strength: {
        baseDamage: this.constants.DAMAGE_PER_STRENGTH,
        controlSuccess: this.constants.CONTROL_SUCCESS_PER_STRENGTH
      },
      agility: {
        speed: this.constants.SPEED_PER_AGILITY,
        critRate: this.constants.CRIT_RATE_PER_AGILITY
      }
    }
    
    return increments[attrType] || {}
  }
  
  /**
   * 数值限制函数
   * @param {number} value - 要限制的值
   * @param {number} min - 最小值
   * @param {number} max - 最大值
   * @returns {number} 限制后的值
   */
  clamp(value, min, max) {
    return Math.max(min, Math.min(max, value))
  }
  
  /**
   * 格式化属性显示
   * @param {number} value - 属性值
   * @param {string} type - 属性类型
   * @returns {string} 格式化后的显示文本
   */
  formatAttributeDisplay(value, type) {
    const percentageTypes = [
      'damageIncrease', 'damageReduction', 'critRate', 
      'abnormalResist', 'controlSuccess', 'dodgeRate', 'hitRate'
    ]
    
    if (percentageTypes.includes(type)) {
      return `${value.toFixed(1)}%`
    }
    
    return Math.floor(value).toString()
  }
}

// 创建单例实例
const attributeCalculator = new AttributeCalculator()

export default attributeCalculator
export { ATTRIBUTE_CONSTANTS, AttributeCalculator }